using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;

namespace YB.EnterpriseLibrary.XSLTManager.Configuration.Design
{
    class XSLTManagerSettingsBuilder
    {
        private XSLTManagerSettingsNode xsltSettingsNode;
        private IConfigurationUIHierarchy hierarchy;
        private XSLTManagerSettings xsltConfiguration;

        public XSLTManagerSettingsBuilder(IServiceProvider serviceProvider, XSLTManagerSettingsNode xsltSettingsNode)
        {
            this.xsltSettingsNode = xsltSettingsNode;
            hierarchy = ServiceHelper.GetCurrentHierarchy(serviceProvider);
            xsltConfiguration = new XSLTManagerSettings();
        }

        internal XSLTManagerSettings Build()
        {
            xsltConfiguration.DefaultXSLTManager = xsltSettingsNode.DefaultXSLTManager.Name;
            if (!xsltSettingsNode.RequirePermission)	// don't set if false
                xsltConfiguration.SectionInformation.RequirePermission = xsltSettingsNode.RequirePermission;

            BuildXSLTManagers();

            return xsltConfiguration;
        }

        private void BuildXSLTManagers()
        {
            foreach (XSLTManagerNode managerNode in hierarchy.FindNodesByType(xsltSettingsNode, typeof(XSLTManagerNode)))
            {
                XSLTManagerData xsltManagerData = managerNode.XSLTManagerData;
                xsltConfiguration.XSLTManagers.Add(xsltManagerData);
            }
        }
    }
}
